package com.xuecheng.content.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xuecheng.content.model.dto.TeachPlanDTO;
import com.xuecheng.content.model.po.Teachplan;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 课程计划 Mapper 接口
 * </p>
 *
 * @author 勾新杰
 * @since 2024-12-21
 */
public interface TeachplanMapper extends BaseMapper<Teachplan> {

    /**
     * 根据课程id查询课程计划<br>
     * <strong>这里先根据parent id保证所有大章节排在小章节前面，用order by排序具体的同级章节之间的顺序</strong>
     * @param courseId 课程id
     * @return 该课程的所有大章节、小章节
     */
    @Select("select * from xcplus0107_content.teachplan t where course_id = #{courseId} order by parentid, orderby;")
    List<TeachPlanDTO> selectByCourseId(Long courseId);

    /**
     * 根据课程id、parent id、grade查询同级章节总数
     * @param courseId 课程id
     * @param parentId 父级id
     * @param grade 层
     * @return 同级章节总数
     */
    @Select("select max(orderby) from xcplus0107_content.teachplan where course_id = #{courseId} and parentid = #{parentId}")
    Integer selectMaxOrder(@Param(value = "courseId") Long courseId, @Param(value = "parentId") Long parentId);

    /**
     * 根据id查询该章节的子章节
     * @param id 章节id
     * @return 该章节的子章节
     */
    @Select("select * from xcplus0107_content.teachplan where parentid = #{id}")
    List<Teachplan> selectByParentId(Long id);

    /**
     * 根据课程id和parent id查询该课程的章节
     * @param courseId 课程id
     * @param parentId 父级id
     * @return 该课程的章节
     */
    @Select("select * from xcplus0107_content.teachplan where course_id = #{courseId} and parentid = #{parentId}")
    List<Teachplan> selectByCourseIdAndParentId(@Param("courseId") Long courseId, @Param("parentId") Long parentId);

    /**
     * 根据课程id删除该课程的章节
     * @param courseId 课程id
     */
    @Delete("delete from xcplus0107_content.teachplan where course_id = #{courseId};")
    void deleteByCourseId(Long courseId);
}
